package window;
/*
Java 练习项目
    Author：叛逆的小羊驼
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;

public class Game01 extends Frame {
    //关于图片路径问题 eclipse无法实现(未找到原因） 。IDAE可以（需放在out文件夹下）
    Image planeima = GameUtil.getImage("images/plane.png");
    Image bg = GameUtil.getImage("images/bg.jpg");
    //Image player =GameUtil.getImage("images/enemy.jpg");
    Plane plane = new Plane(planeima, 250, 250, 6);   //利用构造器初始化
    Bullet bullet[] = new Bullet[80];
    Explode boom;
    Date start = new Date();
    Date end;
    double time;

    @Override
    public void paint(Graphics g)    //画图函数
    {
        super.paint(g);                //不写黑屏z
        Color color = g.getColor();    //画笔颜色
        g.setColor(color.WHITE);
        g.drawImage(bg, 0, 0, null);
        plane.drawself(g);          //参数是画笔
        for (int i = 0; i < bullet.length; i++) {
            bullet[i].drawbillet(g);
            //碰撞检测      错误出在Bullet类中 ，重新定义（重写）了 x，y 导致根类getRec()函数其无法获取其值
            if (bullet[i].getRec().intersects(plane.getRec())) {
                plane.live = false;
                if (boom == null) {
                    boom = new Explode(plane.x, plane.y);
                    end = new Date();
                }
                time = (end.getTime() - start.getTime()) / 1000.0;
                boom.draw(g);
            }
            if (!plane.live) {
                Font font = new Font("宋体", Font.BOLD, 30);
                g.setFont(font);
                g.drawString("时间：" + time + "秒", (Final.WIDTH / 2) - 100, Final.HEIGHT / 2);

            }

        }
        g.setColor(color);
    }

    class Repaint extends Thread {
        @Override
        public void run() {
            while (true) {
                repaint();//重画窗口
                try {
                    Thread.sleep(40);    //40ms 刷新一次
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } //40ms
            }
        }
    }

    //键盘监听事件函数
    class KeyEvents extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            plane.addDirect(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            plane.delDirect(e);
        }
    }

    public void LFrame()                //初始化
    {
        this.setTitle("Plane Game   "); // 标题
        this.setVisible(true); // 可视化
        this.setSize(Final.WIDTH, Final.HEIGHT); // 大小
        this.setLocation(300, 300);
//        JButton button = new JButton("Play!");
//        button.setBounds(20, 20, 20, 20);
//        button.setLocation(100, 100);
//        button.addActionListener(new AbstractAction() {
//            @Override
//            public void actionPerformed(ActionEvent actionEvent) {
//                System.out.println("按下。。。。");
//            }
//        });
//        add(button);
        // 增加监听事件 结束
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        new Repaint().start();//启动重画线程
        addKeyListener(new KeyEvents());

        //初始化炮弹
        //随机位置散射
        double dx = Math.random() * Final.WIDTH, dy = Math.random() * Final.HEIGHT;
        for (int i = 0; i < bullet.length; i++) {
            bullet[i] = new Bullet(Final.SPEED, dx, dy);      //构造器初始化
        }

    }

    public static void main(String[] args) {
        Game01 f = new Game01();
        f.LFrame();
    }

    //双缓冲 解决屏幕闪烁

    @Override
    public void update(Graphics g) {
        Image ImageBuffer = createImage(this.getWidth(), this.getHeight());
        Graphics GraImage = ImageBuffer.getGraphics();
        paint(GraImage);
        GraImage.dispose();
        g.drawImage(ImageBuffer, 0, 0, this);
    }
}


